#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
void sys_err(const char* str)
{
    perror(str);
    exit(1);
}

int main(int argc,char* argv[])
{
    int sockfd=0,new_sockfd=0,ret,i;
    char buff[BUFSIZ];
    struct sockaddr_in server_addr,client_addr;
    socklen_t client_len;
    bzero(&server_addr,sizeof(server_addr));
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(8080);
    server_addr.sin_addr.s_addr=htonl(INADDR_ANY);

    sockfd=socket(AF_INET,SOCK_DGRAM,0);
    if (sockfd<0)
    {
        sys_err("sockfd()");
    }
    bind(sockfd,(const struct sockaddr *)&server_addr,sizeof(server_addr));

    listen(sockfd,12);

    client_len=sizeof(client_addr);
    // new_sockfd=accept(sockfd,(struct sockaddr *)&client_addr,&client_len);
    // if (sockfd<0)
    // {
    //     sys_err("sockfd()");
    // }
    while(1)
    {
        // ret=read(new_sockfd,buff,sizeof(buff));
        // int i=0;
        // write(STDOUT_FILENO,buff,ret);
        // for(i=0;i<ret;i++)
        // {
        //     buff[i]=toupper(buff[i]);
        // }
        // write(new_sockfd,buff,ret);
        ret = recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr*)&client_addr,&client_len);
        write(STDOUT_FILENO,buff,ret);
        for(i = 0;i<ret;++i)
        {
            buff[i] = toupper(buff[i]);
        }
        sendto(sockfd,buff,ret,0,(struct sockaddr*)&client_addr,client_len);
    }
    close(sockfd);
    return 0;
}
